const webpack = require('webpack');
const config = require('./config'); // 导入配置
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const entry = require('./config/entries.json');

// env是判断当前开发环境的变量
var env = process.env.NODE_ENV;
const configEnv = env === 'production' ? config.build.env : config.dev.env;

// 入口配置，设置src/modules下的js入口文件路径
const devEntry = {};
entry.forEach(item => {
  devEntry[item] = {
    // page 的入口
    entry: 'src/modules/' + item + '/' + item + '.js',
    // 模板来源
    template: 'src/modules/' + item + '/' + item + '.html',
    // 在 dist/index.html 的输出
    filename: `${item}.html`,
    // 当使用 title 选项时，
    // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title>
    title: `${item}`,
    // 在这个页面中包含的块，默认情况下会包含
    // 提取出来的通用 chunk 和 vendor chunk。
    chunks: ['chunk-vendors', 'chunk-common', item]
  };
});

const pages = {
  // 默认第一个页面为index
  index: {
    ...devEntry[entry[0]],
    filename: 'index.html',
    chunks: ['chunk-vendors', 'chunk-common', 'index']
  },
  ...devEntry
};

// 入口配置结束

module.exports = {
  lintOnSave: true,
  parallel: false,
  // parallel: true,
  publicPath: env === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath,
  assetsDir: env === 'production' ? config.build.assetsSubDirectory : config.dev.assetsSubDirectory,
  devServer: {
    open: true,
    port: config.dev.port,
    proxy: config.dev.proxyTable,
    overlay: {
      warnings: true,
      errors: true
    }
  },
  pages,
  configureWebpack: {
    resolve: {
      extensions: ['.js', 'css', 'scss', '.vue'],
      // 配置路径别名
      alias: {
        'vue$': 'vue/dist/vue',
        '@': path.resolve(__dirname, 'src'),
        'src': path.resolve(__dirname, 'src'),
        'static': path.resolve(__dirname, 'static'),
        'api': path.resolve(__dirname, 'src/api'),
        'store': path.resolve(__dirname, 'src/store'),
        'pages': path.resolve(__dirname, 'src/pages'),
        '@assets': path.resolve(__dirname, 'src/assets'),
        'utils': path.resolve(__dirname, 'src/utils'),
        'components': path.resolve(__dirname, 'src/components'),
        'constant': path.resolve(__dirname, 'src/constant/constant'),
        'specials': path.resolve(__dirname, 'src/special-components'),
        'tmpl': path.resolve(__dirname, 'src/tmpl'),
        'scss': path.resolve(__dirname, 'src/scss'),
        'libs': path.resolve(__dirname, 'src/libs'),
        'modules': path.resolve(__dirname, 'src/modules')
      }
    },
    plugins: [
      new webpack.DefinePlugin({
        'process.env': configEnv,
        'API_URL': configEnv.API_URL,
        'IMG_URL': configEnv.IMG_URL,
        'SERVICE_URL': configEnv.SERVICE_URL,
        'STATIC': '"/static"',
        'PERFORM_URL': configEnv.PERFORM_URL,
        'PERFORM_ZX_URL': configEnv.PERFORM_ZX_URL,
        'PERFORM_CWURL': configEnv.PERFORM_CWURL,
      }),
      // 复制静态资源
      new CopyWebpackPlugin([
        { from: path.join(__dirname, 'static'), to: 'static' }
      ])
    ]
  }
};
